from sklearn.datasets import load_boston

# 导入波斯顿的房价数据
boston = load_boston()


from sklearn.cross_validation import train_test_split
import numpy as np

# 提取出训练、测试集及目标值
X = boston.data
y = boston.target
# print(X)

# 随机采样25%的数据构建测试样本，其余作为训练样本
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.25)

# 分析回归目标值的差异
# print('目标值最大值：',np.max(y))
# print('目标值最小值:',np.min(y))
# print('目标平均值:',np.mean(y))


#  从sklearn.preprocessing导入数据标准化模块
from sklearn.preprocessing import StandardScaler

# 分别初始化对特征和目标值的标准化模块
ss_X = StandardScaler()
ss_y= StandardScaler()

# 分别对训练和测试数据的特征以及目标值进行标准化处理
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.fit_transform(X_test)
# y_train = ss_y.fit_transform(y_train)
# y_test = ss_y.transform(y_test)
y_train = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test = ss_y.transform(y_test.reshape(-1, 1))

# print(X_train)

# 从sklearn.linear_model导入LinearRegression
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
# 使用训练数据进行参数估计
lr.fit(X_train, y_train)
# 对测试数据进行回归预测
lr_y_predict = lr.predict(X_test)
# print(lr_y_predict)
# print(lr_y_predict)


# 从sklearn.linear_model 导入SGDRegressior
from sklearn.linear_model import SGDRegressor
sgdr = SGDRegressor(max_iter=5)
# sgdr = SGDRegressor()
# 使用默认配置初始化线性回归器SGDRegressor
# sgdr.fit(X_train, y_train)
sgdr.fit(X_train, y_train.ravel())
# 对测试数据进行回归预测
sgdr_y_predict = sgdr.predict(X_test)
# print(sgdr_y_predict)


# 不同于类别的预测，我们不能苛求回归预测的数值结果要严格地与真实值相同。一般情况下，我们希望衡量预测值与真实值之间的差异
# 因此，可以通过多种测评函数进行评价。其中最为直观的评价指标包括，平均绝对误差(Mean Absolute Error, MAE)以及均方差误差(Mean Squared Error， MSE)

# 使用LinearRegression模型自带的评估模型，并输出评估结果
print('LinearRegression精确度为',lr.score(X_test, y_test))

from sklearn.metrics import r2_score
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error

# 使用r2_score模块，并输出评估结果
print('The value of R-squared of LinearRegression is', r2_score(y_test, lr_y_predict))

# 使用mean_squared_error模块，并输出评估结果
print('The mean squared error of LinearRegression is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict)))
# print('The mean squared error of LinearRegression is', mean_squared_error(y_test, sgdr_y_predict))

# 使用mean_absolute_error模块，并输出结果
print('The mean absolute error of LinearRegression is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict)))
# print('The mean absolute error of LinearRegression is', mean_absolute_error(y_test, sgdr_y_predict))